package com.warm.jpa.controller;

import com.warm.jpa.entity.BaseUserEntity;
import com.warm.jpa.repository.BaseRoleRepository;
import com.warm.jpa.repository.BaseUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

/**
 * @Date: 2020/3/31 15:16
 * @Author: LChuang
 * @Description:
 */
@RestController
@RequestMapping(value = "/jpa")
public class JpaController {

    @Autowired
    private BaseUserRepository userRepository;

    @Autowired
    private BaseRoleRepository roleRepository;

    public void baseOpr(){
        /** save */
        BaseUserEntity user1 = new BaseUserEntity();
        user1.setAccount("2520");
        user1.setAge(20);
        user1.setName("warm");
        user1.setSex(1);

        BaseUserEntity saveUser = userRepository.save(user1);

        saveUser.setName("chuang");
        /** 更新操作通过 save 实现 */
        userRepository.save(saveUser);

        Optional<BaseUserEntity> user2 = userRepository.findById(user1.getId());

        /** 自定义查询 */
        Optional<BaseUserEntity> user22 = Optional.ofNullable(userRepository.findByAccount("warm"));
        List<BaseUserEntity> userList = userRepository.findByAgeGreaterThan(20);

    }

    public void update(){

    }
}
